package com.example.banksys;

import DataBase.myDB;
import handler.arexcelUtil;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.AnchorPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;

public class ListOptionController {

    public Button confirm;
    public AnchorPane pane;

    class item{
        public String choice;
        public int daytime;

        public item(String choice,int daytime){
            this.choice=choice;
            this.daytime=daytime;
        }

        public item(){}
    }

    public ArrayList<item> choiceList;
     public ChoiceBox<String> duration_choice;

     String dirPath;
     int day;

     public void initialize(){
         choiceList=new ArrayList<>();
         item i1=new item("一个月",30);
         item i2=new item("三个月",90);
         item i3=new item("六个月",180);
         item i4=new item("一年",365);
         choiceList.add(i1);
         choiceList.add(i2);
         choiceList.add(i3);
         choiceList.add(i4);
         duration_choice.getItems().addAll("一个月","三个月","六个月","一年");
         confirm.setDisable(true);
         duration_choice.setOnAction(new EventHandler<ActionEvent>() {
             @Override
             public void handle(ActionEvent actionEvent) {
                 if(duration_choice.getValue() == null && duration_choice.getValue().isEmpty() && duration_choice.getValue().equals("")){
                     confirm.setDisable(true);
                 }else{
                     confirm.setDisable(false);
                     for(int i=0;i<4;i++){
                         if(duration_choice.getValue().equals(choiceList.get(i).choice)){
                             day=choiceList.get(i).daytime;
                             break;
                         }
                     }
                 }

             }
         });
         confirm.setOnAction(new EventHandler<ActionEvent>() {
             @Override
             public void handle(ActionEvent actionEvent) {
                 DirectoryChooser t=new DirectoryChooser();
                 File choosenDir=t.showDialog(new Stage());
                 dirPath=choosenDir.getPath();
                 Timestamp currentT  = new Timestamp(System.currentTimeMillis());
                 Calendar c = Calendar.getInstance();
                 c.setTime(currentT);
                 c.add(Calendar.DATE, (-day));  //  加一  天
                 //c.add(Calendar.MONTH, 1); //  加一个月
                 //c.add(Calendar.YEAR,1);   //  加一  年
                 Timestamp limitedT = new Timestamp(c.getTimeInMillis());
                 String sql="select * from trade_record natural join trade_type where trade_date > '"+limitedT+"' and account_ID="+QueryPageController.acc_ID;
                 myDB query=new myDB("banksystem");
                 try {
                     ResultSet res=query.getStatement().executeQuery(sql);
                     arexcelUtil handler=new arexcelUtil();
                     handler.genarateExcel(res,dirPath);
                 } catch (Exception e) {
                     throw new RuntimeException(e);
                 }
                 Alert alert = new Alert(Alert.AlertType.INFORMATION);
                 alert.titleProperty().set("信息");
                 alert.headerTextProperty().set("success");
                 alert.showAndWait();
                 pane.getScene().getWindow().hide();


             }
         });
     }
}
